package co.edu.usbcali.sam.dataaccess.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import co.edu.usbcali.sam.dataaccess.api.HibernateDaoImpl;
import co.edu.usbcali.sam.modelo.Usuario;
import co.edu.usbcali.sam.modelo.dto.UsuarioDTO;


/**
 * A data access object (DAO) providing persistence and search support for
 * Usuario entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @see lidis.Usuario
 */
@Scope("singleton")
@Repository("UsuarioDAO")
public class UsuarioDAO extends HibernateDaoImpl<Usuario, Long>
    implements IUsuarioDAO {

	private static final Logger log = LoggerFactory.getLogger(UsuarioDAO.class);
    @Resource
    private SessionFactory sessionFactory;

    public static IUsuarioDAO getFromApplicationContext(ApplicationContext ctx) {
        return (IUsuarioDAO) ctx.getBean("UsuarioDAO");
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<UsuarioDTO> consultarUsuariosNombreRol() throws Exception {
		
		List<UsuarioDTO> list = null;
		
		try {
	
			Query query = getSession().getNamedQuery("consultar_usuarios_rol_persona_por_usuario");
			query.setResultTransformer(Transformers.aliasToBean(UsuarioDTO.class));
			list = query.list();

			
		} catch (Exception e) {
			throw e;
		}
		
		return list;
	}
    
	@SuppressWarnings("unchecked")
	@Override
	public Long consultarIdEstudiantexLogin(String login) throws Exception{
		
   		Long cant = 0L;
			try {
			Query query = getSession().getNamedQuery("consultar_idestudiante_por_login");
			query.setParameter("pLogin", login);
			List<Long> lista = query.list();

			if (lista!=null) {
				cant =lista.get(0);
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return cant;
	}
}
